﻿using com.nm.library.Excels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nm.library;
using System.Data;
using System.ComponentModel.Composition;
using com.nm.db.Models.Terminal;
using com.nm;
using com.nm.library.Helper;
using george.Helper;

namespace TerminalAPI.ImportExcel
{
    [Export(typeof(ExcelImport))]
    public class DisplayShelfDetailExcel : ExcelImport
    {
        public override Dictionary<string, ImportVerify> DictFields
        {
            get
            {
                return dictFields;
            }
        }

        private static Dictionary<string, ImportVerify> dictFields = new List<ImportVerify> {
            new ImportVerify{ ColumnName="货架编号",FieldName="GSCode",DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true) },
            new ImportVerify{ ColumnName="货架名称",FieldName="GSName",DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,30,true,true) },
            new ImportVerify{ ColumnName="系列",FieldName="Series",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,true,true)},
            new ImportVerify{ ColumnName="商品层",FieldName="CommodityLayer" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,true,false)},
            new ImportVerify{ ColumnName="品类",FieldName="Category" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,true,false)},
            new ImportVerify{ ColumnName="最低出样件数",FieldName="Minimum",DataType=typeof(int),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,6,true,false)},
            new ImportVerify{ ColumnName="标准出样件数",FieldName="Avgximum",DataType=typeof(int),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,6,true,false)},
             new ImportVerify{ ColumnName="最高出样件数",FieldName="Maximum",DataType=typeof(int),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,6,true,false)}
        }.ToDictionary(e => e.ColumnName, e => e);


        public override ExcelImportType Type => ExcelImportType.SHELFDISPLAYDETAIL;

        public override object SaveImportData(DataTable dt, Dictionary<string, object> extraInfo, int userId)
        {
            var index = dt.Rows.Count;
            var msg = $"成功导入{index}条数据";
            try
            {
                var dc = new TerminalEntities();
                foreach (DataRow item in dt.Rows)
                {
                    var gsCode = item["GSCode"].ToString().StartTrim();
                    var good = (from g in dc.Tm_ShelfDisplay
                                join m in dc.Tm_GoodShelf 
                                on g.GSID equals m.GSID
                                where m.GSCode==gsCode
                                select new { g.GSID, m.GSCode,g.SDID }).FirstOrDefault();
                    if (good == null)
                    {
                        SysChangeLog.WriteLog(0, 3, gsCode, "货架陈列明细", $"货架编号:{gsCode}不存在");
                        continue;
                    }
                    var query = dc.Tm_ShelfDisplayDetail.FirstOrDefault(t => t.SDID == good.SDID);
                    var maximum = Convert.ToInt16(item["Maximum"].ToString());
                    var avgximum = Convert.ToInt16(item["Avgximum"].ToString());
                    var minimum = Convert.ToInt16(item["Minimum"].ToString());
                    if (query != null)
                    {
                        query.Maximum = maximum;
                        query.Avgximum = avgximum;
                        query.Minimum = minimum;
                        query.Series = item["Series"].ToString();
                        query.CommodityLayer = item["CommodityLayer"].ToString();
                        query.Category = item["Category"].ToString();
                    }
                    else
                    {
                        var detail = new Tm_ShelfDisplayDetail
                        {
                            Maximum = maximum,
                            Avgximum = avgximum,
                            Minimum = minimum,
                            Series = item["Series"].ToString(),
                            CommodityLayer = item["CommodityLayer"].ToString(),
                            Category = item["Category"].ToString(),
                            SDID = good.SDID
                        };
                        dc.Tm_ShelfDisplayDetail.Add(detail);
                    }
                }
                dc.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return new ImportResult()
                {
                    IsSuccess = false,
                    ExtraInfo = ex,
                    Message = ex.Message
                };
            }
            return new ImportResult()
            {
                IsSuccess = true,
                ExtraInfo = "导入成功",
                Message = msg
            };
        }

        public override DataTable GetExportData(string Keys)
        {
            var dc = new TerminalEntities();
            var query = (from shelf in dc.Tm_ShelfDisplay
                         join good in dc.Tm_GoodShelf on shelf.GSID equals good.GSID
                         where good.IsRemove==false && shelf.IsRemove==false
                         select new {
                             good.GSCode,good.GSName
                         }).ToList();
            return DataTableHelper.LinqToDataTable(query);
        }
    }
}
